import axios from './base.js'

import system from './system.js'
import { MD5 } from 'crypto-js'
import { genAesKey, str2Base64, encryptAes, encryptData } from './encode.js'

export default {
  auth: {
    login(params = {}) {
      const { username } = params
      let password = MD5(params.password).toString()

      let data = {
        username,
        password,
        grantType: 'password',
        clientId: window.$app.clientId
      }

      const k = genAesKey()
      const ek = encryptAes(str2Base64(k))
      data = encryptData(data, k)

      return axios({
        url: '/auth/login',
        method: 'post',
        data,
        headers: {
          'Encrypt-Key': ek,
          Isencrypt: true,
          'Content-Type': 'application/json;charset=UTF-8'
        }
      })
    }
  },
  system,
  
  searchUserByName: (params = {}) => {
    return axios({
      url: '/auth/searchUserByName',
      method: 'get',
      params
    })
  }
}
